package musicstore.action;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import musicstore.biz.UserBiz;
import musicstore.entity.User;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private String username;
	private String password;

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	// 错误信息
	private String error;

	public String getError() {
		return error;
	}

	private UserBiz userBiz;
	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}

	public String checkUser() {
		HttpSession sess = ServletActionContext.getRequest().getSession();
		User user = userBiz.fetchUser(this.username, this.password);
		sess.setAttribute("user", user);
		this.error = "";
		if (user != null) {
			return SUCCESS;
		} else {
			this.error = "用户名或者密码错误";
			return LOGIN;
		}
	}
	
	private User user;
	public void setUser(User user) {
		this.user = user;
	}
	public User getUser() {
		return user;
	}

	public String register(){
		this.user.setUserRoleId(1);;
		this.user.setIsInUse(1);
		userBiz.add(this.user);
		return SUCCESS;
	}
	
	
	public String checkUsername() throws IOException{
		userBiz.fetchUserByUsername(user.getUsername());
		String result =null;
		if(userBiz.fetchUserByUsername(user.getUsername()))
			result="false";
		else
			result = "true";
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.getWriter().print(result);
		return null;
	}

}
